﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog.LayoutRenderers;
using NPOI.XSSF.UserModel;
using Web.IBLL;
using Web.Model;

namespace WebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoomNumController : ControllerBase
    {
        public RoomNumController(IRNumBLL bLL)
        {
            BLL = bLL;
        }

        public IRNumBLL BLL { get; }

        [HttpPost]
        public Task<int> RNAdd(RoomNumClass n)
        {
            return BLL.RNAdd(n);
        }
        [HttpGet]
        public dynamic RShow(int Pageindex, int Pagesize)
        {
            return BLL.RShow(Pageindex, Pagesize);
        }
        [HttpGet]
        public IActionResult DAochu()
        {
            List<RoomNumClass> list = BLL.RNShow();
            XSSFWorkbook wb = new XSSFWorkbook();
            var sheet = wb.CreateSheet("房号表");
            var row = sheet.CreateRow(0);
            int num = 1;
            row.CreateCell(0).SetCellValue("房间编号");
            row.CreateCell(0).SetCellValue("房间号");
            row.CreateCell(0).SetCellValue("房间楼层");
            row.CreateCell(0).SetCellValue("房间状态");

            foreach (var h in list)
            {
                var rows = sheet.CreateRow(num);
                row.CreateCell(0).SetCellValue(h.Id);
                row.CreateCell(1).SetCellValue(h.Num);
                row.CreateCell(2).SetCellValue(h.Lou);
                row.CreateCell(3).SetCellValue(h.State);
                num++;
            }

            MemoryStream ms = new MemoryStream();
            wb.Write(ms);
            byte[] bytes = ms.ToArray();
            return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "房号信息表.xlsx");


        }


    }
}
